﻿package _5_ProgressBarAndSlider;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
/**
 * 2011-6-5 14:08:32
 * @author Administrator
 */
public class TJProgressBar {
    JFrame frame;
    Container contentPane;
    JProgressBar progressBar;
    JButton startButton,pauseButton,stopButton;
    MyThread thread;
    public TJProgressBar(){
        frame=new JFrame("进度条测试");
        contentPane=frame.getContentPane();
        progressBar=new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
        progressBar.setStringPainted(true);
        progressBar.setValue(0);
        progressBar.setPreferredSize(new Dimension(400,30));
        thread=new MyThread();
        startButton=new JButton("开始");
        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                thread.start();
                startButton.setEnabled(false);
                pauseButton.setEnabled(true);
            }
        });
        pauseButton=new JButton("暂停");
        pauseButton.setEnabled(false);
        pauseButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(pauseButton.getText().equals("暂停")){
                    thread.pause();
                    pauseButton.setText("继续");
                }
                else{
                    thread._continue();
                    pauseButton.setText("暂停");
                }
            }
        });
        stopButton=new JButton("停止");
        stopButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                thread.stop();
                startButton.setEnabled(true);
                pauseButton.setEnabled(false);
                pauseButton.setText("暂停");
            }
        });
        Box box=Box.createVerticalBox();
        box.add(Box.createVerticalStrut(10));
        box.add(progressBar);
        box.add(Box.createVerticalStrut(10));
        Box box2=Box.createHorizontalBox();
        box2.add(Box.createHorizontalGlue());
        box2.add(startButton);
        box2.add(Box.createHorizontalStrut(10));
        box2.add(pauseButton);
        box2.add(Box.createHorizontalStrut(10));
        box2.add(stopButton);
        box2.add(Box.createHorizontalGlue());
        box.add(box2);
        box.add(Box.createVerticalStrut(10));
        contentPane.add(box);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public static void main(String args[]){
        new TJProgressBar();
    }
    class MyThread{
        Thread thread;
        volatile boolean _continue;
        volatile boolean _stop;
        volatile int value;
        public MyThread(){
            _continue=false;
            value=0;
            _stop=false;
        }
        public void reset(){
            _continue=true;
            _stop=false;
        }
        public void start(){
            reset();
            if(thread==null||!thread.isAlive()){
                thread=new Thread(){
                    public void run(){
                        while(true){
                            if(_stop){
                                break;
                            }
                            if(_continue){
                                progressBar.setValue(value);
                                value++;
                                value%=100;
                            }                            
                            try{
                                Thread.sleep(100);
                            }catch(InterruptedException e){}
                        }
                    }
                };
                thread.start();
            }
        }
        public void pause(){
            _continue=false;
        }
        public void _continue(){
            _continue=true;
        }
        public void stop(){
            _stop=true;
            value=0;
            progressBar.setValue(0);
        }
    }
}
